package com.wyl.filter

import com.wyl.utils.USER_SESSION
import javax.servlet.*
import javax.servlet.annotation.WebFilter
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

@WebFilter("/jsp/*")
class SysFilter : Filter {
    override fun doFilter(request: ServletRequest, response: ServletResponse, chain: FilterChain) {
        val req = request as HttpServletRequest
        val resp = response as HttpServletResponse
        println("SysFilter --> ${req.contextPath} - ${req.servletPath}")
        val user = req.session.getAttribute(USER_SESSION)
        if (user == null) {
            //已经被移除或者注销了，或者未登录
            resp.sendRedirect(req.contextPath + "/error.jsp")
        } else {
            chain.doFilter(req, resp)
        }
    }

    override fun destroy() {
    }

    override fun init(filterConfig: FilterConfig?) {
    }
}